/**
 * @description Add the listener to drag the source Component
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.util;

public class GroupingPanel extends javax.swing.JPanel
       implements java.awt.dnd.DropTargetListener, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {

 protected javax.swing.border.Border outerBorder;
 protected javax.swing.border.Border innerBorder;
 protected javax.swing.border.Border border;

 // private class PartialLineBorder
 private class PartialLineBorder extends javax.swing.border.AbstractBorder {
  protected java.awt.Color color;
  protected int thickness;
  protected boolean includeTop;
  protected boolean includeLeft;
  protected boolean includeBottom;
  protected boolean includeRight;

  public PartialLineBorder (java.awt.Color color, int thickness, boolean includeTop, boolean includeLeft, boolean includeBottom, boolean includeRight) {
   this.color = color;
   this.thickness = thickness;
   this.includeTop = includeTop;
   this.includeLeft = includeLeft;
   this.includeBottom = includeBottom;
   this.includeRight = includeRight;
   }

  //Rem @Override
  public void paintBorder (java.awt.Component c, java.awt.Graphics g, int x, int y, int width, int height) {
   java.awt.Color oldColor = g.getColor();
   g.setColor( color );
   for (int i = 0; i<thickness; i++) {
    if( includeTop) {
     g.drawLine( x, y + i, x + width - 1, y + i );
     }
    if( includeLeft) {
     g.drawLine( x + i, y, x + i, y + height - 1 );
     }
    if( includeBottom) {
     g.drawLine( x, y - i + height - 1, x + width - 1, y - i + height - 1 );
     }
    if( includeRight) {
     g.drawLine( x - i + width - 1, y, x - i + width - 1, y + height - 1 );
     }
    }
   g.setColor( oldColor );
   }

  //Rem @Override
  public java.awt.Insets getBorderInsets (java.awt.Component c) {
   return new java.awt.Insets( includeTop ? thickness : 0, includeLeft ? thickness : 0, includeBottom ? thickness : 0, includeRight ? thickness : 0 );
   }

  //Rem @Override
  public java.awt.Insets getBorderInsets (java.awt.Component c, java.awt.Insets insets) {
   insets.top = includeTop ? thickness : 0;
   insets.left = includeLeft ? thickness : 0;
   insets.bottom = includeBottom ? thickness : 0;
   insets.right = includeRight ? thickness : 0;
   return insets;
   }

  public java.awt.Color getLineColor () {
   return color;
   }

  public int getThickness () {
   return thickness;
   }

  //Rem @Override
  public boolean isBorderOpaque () {
   return true;
   }
  } // private class PartialLineBorder extends javax.swing.border.AbstractBorder

private static int staticCont=0;
public int cont = -1;
public java.awt.dnd.DropTarget dropTarget = null; //L

 // Chamado em: estendido por 'edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.VariableGUI.<init>(VariableGUI.java:20)'
 //             estentido por 'edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.<init>(DnDGroupingPanel.java:44)'
 // 
 //
 // edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementPrototypeDnDPanel$Tile.<init>(ElementPrototypeDnDPanel.java:148)
 // 216 edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor.CompositeElementEditor.<init>(CompositeElementEditor.java:38)
 // edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.<init>(DnDGroupingPanel.java:44)
 // edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.<init>(DnDGroupingPanel.java:44)
 // edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.<init>(DnDGroupingPanel.java:44)
 // 220: idem
 // edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementPrototypeDnDPanel.<init>(ElementPrototypeDnDPanel.java:19)
 // edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ElementPrototypeDnDPanel$Tile.<init>(ElementPrototypeDnDPanel.java:148)
 // edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.<init>(DnDGroupingPanel.java:44)
 public GroupingPanel () {
   //T if (staticCont==223) { // edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.getStaticCont()==1) {
   //T  try{String str=""; System.out.println(str.charAt(3));}catch(Exception e) {e.printStackTrace();}
   //T  System.out.println("GroupingPanel.java: GroupingPanel(): "+cont+": getParent()="+getParent());
   //T  }
   // System.out.println("GroupingPanel.java: GroupingPanel(): "+cont+": #variavel="+edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel.getStaticCont());
this.cont = staticCont++;
  outerBorder = new PartialLineBorder( java.awt.Color.lightGray, 1, false, false, true, true );
  innerBorder = javax.swing.BorderFactory.createEmptyBorder( 1, 1, 1, 1 );
  border = javax.swing.BorderFactory.createCompoundBorder( outerBorder, innerBorder );
  setBorder( border );
  setDoubleBuffered( true );

  //setOpaque( false );

  // java.awt.Component: public synchronized void setDropTarget(DropTarget dt)
  // java.awt.dnd.DropTarget: public DropTarget(Component c, DropTargetListener dtl)
  dropTarget = new java.awt.dnd.DropTarget( this, this );  //L
  setDropTarget(dropTarget);
  //L setDropTarget( new java.awt.dnd.DropTarget( this, this ) ); // to define a listener to this as target of any "drag-drop"

  // edu.cmu.cs.stage3.alice.authoringtool.util.EditorUtilities.editObject(EditorUtilities.java:92)

  addContainerListener (
    new java.awt.event.ContainerAdapter() {
     //Rem @Override
     public void componentAdded (java.awt.event.ContainerEvent ev) {
      if (ev.getChild().getDropTarget() == null) { // is this heavy-handed?
       ev.getChild().setDropTarget( new java.awt.dnd.DropTarget( ev.getChild(), GroupingPanel.this ) );
       }
      }
     //Rem @Override
     public void componentRemoved (java.awt.event.ContainerEvent ev) { //MEMFIX
      if (ev.getChild().getDropTarget() != null) { // is this heavy-handed?
       ev.getChild().getDropTarget().setActive( false );
       ev.getChild().setDropTarget( null );
       }
      }
     }
   );
  addContainerListener(GUIElementContainerListener.getStaticListener());

  }

 public void release () {
  removeAll(); //MEMFIX
  }

 public void paintBackground (java.awt.Graphics g) {  }

 public void paintForeground (java.awt.Graphics g) {  }

 //Rem @Override
 public void paintComponent (java.awt.Graphics g) {
  super.paintComponent( g );
  paintBackground( g );
  }

 //Rem @Override
 public void paintChildren (java.awt.Graphics g) {
  super.paintChildren( g );
  paintForeground( g );
  }

 //Rem @Override
 public void printComponent (java.awt.Graphics g) {
  super.printComponent( g );
  }

 // might get in trouble for going to *any* Container that's a DropTargetListener.  we'll see...
 // at java.awt.dnd.DropTarget.dragEnter(DropTarget.java:337) -> GroupingPanel.dragEnter(java.awt.dnd.DropTargetDragEvent)
 // Erro no Java 1.6:
 // - deveria se: 'getParent()=GroupingPanel.java: dragEnter(DropTargetDragEvent): cont=1:
 //                     edu.cmu.cs.stage3.alice.authoringtool.viewcontroller.ResponsePrintViewController[...]'
 // - chega: dragEnter(DropTargetDragEvent): cont=1: javax.swing.JPanel[...,layout=edu.cmu.cs.stage3.alice.authoringtool.editors.compositeeditor.CompositeElementEditor$2, ...]
 //          dragEnter(DropTargetDragEvent): cont=1: javax.swing.JTabbedPane[...layout=javax.swing.plaf.metal.MetalTabbedPaneUI$TabbedPaneLayout,...]
 public void dragEnter (java.awt.dnd.DropTargetDragEvent dtde) {
  //T String str = edu.cmu.cs.stage3.util.StrUtilities.getComponentName(getParent());
  // int tam=str.length(); if (tam>30) str=str.substring(0,30);
  if (getParent() instanceof java.awt.dnd.DropTargetListener) {
   // System.out.println("\nGroupingPanel.java: dragEnter(DropTargetDragEvent): cont="+this.cont+": "+str); // getParent="+getParent()
   ((java.awt.dnd.DropTargetListener)getParent()).dragEnter( dtde );
  } else {
   //T try { System.err.println("\nError: GroupingPanel.java: dragEnter(DropTargetDragEvent): cont="+this.cont+": "+str); }
   //T catch(Exception e) { System.err.println("Error: GroupingPanel.java: dragEnter(DropTargetDragEvent):"); e.printStackTrace(); }
   // System.out.println("\nGroupingPanel.java: dragEnter(DropTargetDragEvent): cont="+this.cont+": "+tam+": rejected!!");
   //T System.out.println("\nGroupingPanel.java: dragEnter(DropTargetDragEvent): cont="+this.cont+": "+str+": rejected!!");
   dtde.rejectDrag();
   }
  }

 public void dragExit (java.awt.dnd.DropTargetEvent dte) {
  if (getParent() instanceof java.awt.dnd.DropTargetListener) {
   ((java.awt.dnd.DropTargetListener)getParent()).dragExit( dte );
   }
  }

 public void dragOver (java.awt.dnd.DropTargetDragEvent dtde) {
  if (getParent() instanceof java.awt.dnd.DropTargetListener) {
   ((java.awt.dnd.DropTargetListener)getParent()).dragOver( dtde );
  } else {
   dtde.rejectDrag();
   }
  }

 //L actionlistener GroupingPanelDragGestureListener
 public void actionPerformed (java.awt.event.ActionEvent actionEv) {
   // java.awt.dnd.DropTargetDragEvent: ((java.awt.dnd.DropTargetListener)getParent()).dragEnter( dtde );
   Object obj = actionEv.getSource();
   int modifiers = actionEv.getModifiers(); // the modifier keys held down during this action
   String actionCommand = actionEv.getActionCommand(); // 
   String paramString = actionEv.paramString(); //
   System.out.println("GroupingPanel.java: actionPerformed: "+modifiers+", actionCommand="+actionCommand+", paramString="+paramString+": "+
          edu.cmu.cs.stage3.util.StrUtilities.getComponentName(obj));
   }

 public void drop (java.awt.dnd.DropTargetDropEvent dtde) {
  if (getParent() instanceof java.awt.dnd.DropTargetListener) {
   ((java.awt.dnd.DropTargetListener)getParent()).drop( dtde );
  } else {
   dtde.rejectDrop();
   }
  }

 public void dropActionChanged (java.awt.dnd.DropTargetDragEvent dtde) {
  if( getParent() instanceof java.awt.dnd.DropTargetListener) {
   ((java.awt.dnd.DropTargetListener)getParent()).dropActionChanged( dtde );
  } else {
   dtde.rejectDrag();
   }
  }

 }
